// 客户管理：新增与修改model
import { message } from 'antd';
import {
  getRespUserList,
  getCustList,
  getCustListByCode,
  getAreaList,
  getBasciInfo,
  basicUpdateSubmit,
  getInvoiceList,
  invoiceDel,
  getInvoiceInfo,
  invoiceSubmit,
  uploadDownFile,
  getMailList,
  deleteMail,
  getMailInfo,
  insertSubmit,
  updateSubmit,
  getBankList,
  deleteBankAccount,
  getBankAccount,
  bankInsert,
  bankUpdate,
  getContactsList,
  deleteContacts,
  getContacts,
  contactInsert,
  contactUpdate,
  downloadTemplateContacts,
  getCostCenter,
  delCostCenter,
  costCenterInsert,
  checkSubmit,
} from '@/services/custedit';

export default {
  state: {
    respUserList: [],
  },
  effects: {
    // 获取负责人列表
    *fetchRespUserList({ payload }, { call }) {
      const response = yield call(getRespUserList, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取负责人数据：服务端错误');
      return false;
    },
    // 模糊查询客户列表
    *searchCust({ payload }, { call }) {
      const response = yield call(getCustList, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取客户数据：服务端错误');
      return false;
    },
    // 通过社会统一信用代码查询查询客户列表
    *searchCustByCode({ payload }, { call }) {
      const response = yield call(getCustListByCode, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取客户数据：服务端错误');
      return false;
    },
    // 获取省市区列表
    *getAreaList({ payload }, { call }) {
      const response = yield call(getAreaList, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取地区数据：服务端错误');
      return false;
    },
    // 获取基本详情数据
    *getBasciInfo({ payload }, { call }) {
      const response = yield call(getBasciInfo, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取客户信息：服务端错误');
      return false;
    },
    // 修改提交前的校验
    checkSubmit:[
      function* ({ payload }, { call }) {
        const response = yield call(checkSubmit, payload);
        return response;
      },
      {type:'throttle',ms:10000}
    ],
    // 基本信息提交数据
    submit:[
      function* ({ payload }, { call }) {
        const response = yield call(basicUpdateSubmit, payload);
        return response;
      },
      {type:'throttle',ms:10000}
    ],
    // 发票信息：列表
    *getInvoiceList({ payload }, { call }) {
      const response = yield call(getInvoiceList, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取发票信息：服务端错误');
      return false;
    },
    // 发票信息：删除
    *invoiceDel({ payload }, { call }) {
      const response = yield call(invoiceDel, payload);
      return response;
    },
    // 发票信息：详情
    *getInvoiceInfo({ payload }, { call }) {
      const response = yield call(getInvoiceInfo, payload);
      if (response.success) {
        return response;
      }
      message.warning(response.message || '获取发票信息：服务端错误');
      return false;
    },
    // 发票信息提交记录
    invoiceSubmit:[
      function*(
      {
        payload: { submitUrl, submitData },
      },
      { call }
      ) {
        const response = yield call(invoiceSubmit, submitUrl, submitData);

        return response;
      },
      {type:'throttle',ms:10000}
    ],
    // 上传数据 下载模板
    uploadDownFile:[
      function* ({ payload }, { call }) {
        const response = yield call(uploadDownFile, payload);
        return response;
      },
      {type:'throttle',ms:10000}
    ],
    // 邮寄信息地址 获取列表数据
    *getMailList({ payload }, { call }) {
      const response = yield call(getMailList, payload);
      return response;
    },
    // 邮寄信息地址 删除某一条数据
    *deleteMail({ payload }, { call }) {
      const response = yield call(deleteMail, payload);
      return response;
    },
    // 邮寄信息地址 获取修改信息
    *getMailInfo({ payload }, { call }) {
      const response = yield call(getMailInfo, payload);
      return response;
    },
    // 邮寄信息地址 提交新增接口
    insertSubmit:[
      function* ({ payload }, { call }) {
        const response = yield call(insertSubmit, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 邮寄信息地址 提交修改接口
    updateSubmit:[
      function *({ payload }, { call }) {
        const response = yield call(updateSubmit, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 获取银行列表数据
    *getBankList({ payload }, { call }) {
      const response = yield call(getBankList, payload);
      return response;
    },
    // 银行列表删除某一条数据
    *deleteBankAccount({ payload }, { call }) {
      const response = yield call(deleteBankAccount, payload);
      return response;
    },
    // 银行列表回显数据
    *getBankAccount({ payload }, { call }) {
      const response = yield call(getBankAccount, payload);
      return response;
    },
    // 银行新增提交数据
    bankInsert:[
      function* ({ payload }, { call }) {
        const response = yield call(bankInsert, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 银行修改提交数据
    bankUpdate:[
      function* ({ payload }, { call }) {
        const response = yield call(bankUpdate, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 联系记录列表数据
    *getContactsList({ payload }, { call }) {
      const response = yield call(getContactsList, payload);
      return response;
    },
    // 联系记录删除某一条数据
    *deleteContacts({ payload }, { call }) {
      const response = yield call(deleteContacts, payload);
      return response;
    },
    // 联系记录回显数据
    *getContacts({ payload }, { call }) {
      const response = yield call(getContacts, payload);
      return response;
    },
    // 联系记录新增提交数据
    contactInsert:[
      function*({ payload }, { call }) {
        const response = yield call(contactInsert, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 联系记录修改提交数据
    contactUpdate:[
      function* ({ payload }, { call }) {
        const response = yield call(contactUpdate, payload);
        return response;
      },
      { type:'throttle',ms:10000 }
    ],
    // 联系记录上传文件
    *downloadTemplateContacts({ payload }, { call }) {
      const response = yield call(downloadTemplateContacts, payload);
      return response;
    },
    // 获取成本中心列表数据
    *getCostCenter({ payload }, { call }) {
      const response = yield call(getCostCenter, payload);
      return response;
    },
    // 成本中心删除数据
    *delCostCenter({ payload }, { call }) {
      const response = yield call(delCostCenter, payload);
      return response;
    },
    // 成本中心提交数据
    *costCenterInsert({ payload }, { call }) {
      const response = yield call(costCenterInsert, payload);
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
